package com.lc.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 拦截异常
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    //拦截Exception异常
    @ExceptionHandler(Exception.class)
    @ResponseBody //用json方式返回
    public RestErrorCode processException(Exception e) {
        //该异常是否包含自定义异常
        if (e instanceof BusinessException) {
            //强转
            BusinessException businessException = (BusinessException) e;
            ErrorCode errorCode = businessException.getErrorCode();
            return new RestErrorCode(errorCode.getCode(), errorCode.getDesc());
        }
        e.printStackTrace();
        return new RestErrorCode(9999, "未知错误");
    }
}
